package com.google.code.p.restrpc.event;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class RpcEventCenter {
    
    protected static class SingletonHolder {
        static RpcEventCenter instance = new RpcEventCenter();
    }
    
    public static RpcEventCenter getInstance() {
        return SingletonHolder.instance;
    }
    
    private List<RpcEventListener> listeners = new CopyOnWriteArrayList<RpcEventListener>();
    
    public RpcEventCenter() {
    }
    
    public void registerListener(RpcEventListener listener) {
        listeners.add(listener);
    }
    
    public void unregisterListener(RpcEventListener listener) {
        listeners.remove(listener);
    }
    
    public void publishEvent(RpcEventObject eventObject) {
        for (RpcEventListener listener : listeners) {
            if (listener.support(eventObject)) {
                listener.handle(eventObject);
            }
        }
    }
}
